<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2184 – Quote arguments to unset so they're not glob expanded.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2184 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2184">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="quote-arguments-to-unset-so-theyre-not-glob-expanded">Quote
arguments to unset so they're not glob expanded.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2184.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">unset</span> <span class="va">foo[</span>index<span class="op">]</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2184.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="bu">unset</span> <span class="st">&#39;foo[index]&#39;</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>Arguments to <code>unset</code> are subject to regular glob
expansion. This is especially relevant when unsetting indices in arrays,
where <code>[..]</code> is considered a glob character group.</p>
<p>In the problematic code, having a file called <code>food</code> in
the current directory will result in <code>unset foo[index]</code>
expanding to <code>unset food</code>, which will silently succeed
without unsetting the element.</p>
<p>Quoting so that the <code>[..]</code> is passed literally to
<code>unset</code> solves the issue.</p>
<p>Note that you can unset element using variable for index name like
this:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2184.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="bu">unset</span> <span class="st">&#39;foo[$var]&#39;</span></span></code></pre></div>
<h3 id="exceptions">Exceptions:</h3>
<p>If you know that pathname expansion is disabled you can ignore this
message. <code>set -o noglob</code> (and variations like invoking the
script with <code>#!/bin/bash -f</code>) will prevent glob expansion of
arguments to <code>unset</code>.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


